test(File,N1,N2) :-
	see(File),
	get_code(N1),
	get_code(N2),
	close(File).

read_target(File) :-
	see(File),
	read_tokens(Words),

	close(File).


letter_type(Ch,Type) :- 
	space(Ch)->Type=space;
	alphabet(Ch)->Type=alphabet;
	digit(Ch)->Type=digit;
	eof(Ch)->Type=eof;
	Type=other.

space(Ch) :-
	Ch =:= " ";
	Ch =:= "\t";
	Ch =:= "\n";
	Ch =:= "\r".

alphabet(Ch) :-
	Ch >= "a", Ch =< "z";
	Ch >= "A", Ch =< "Z".

digit(Ch) :-
	Ch >= "0", Ch =< "9".

eof(Ch) :- Ch < 0.

read_words(WordList) :-
	read_letters(Hole-Hole,Letters,Type,NewLastCh),
	atom_codes(Word, Letters),
	read_words([Word|NewHole]-NewHole,WordList,NewLastCh).

read_words(WordOpenList-Hole,WordList,LastCh) :-
	letter_type(LastCh,LastType),
	(
		LastType==eof -> (WordList-[]=WordOpenList-Hole);
		(
			Type=LastType,
			read_letters([LastCh|ChHole]-ChHole,Letters,Type,NewLastCh),
			atom_codes(Word, Letters),
			diff_append(WordOpenList-Hole,[Word|Hole1]-Hole1,NewWordOpenList-NewHole),
			read_words(NewWordOpenList-NewHole,WordList,NewLastCh)
		)
	).


read_letters(OpenList-Hole,Ans,Type,LastCh) :-
	get_code(Ch),
	letter_type(Ch,ChType),
	(
		ChType=Type ->
		(
			diff_append(OpenList-Hole,[Ch|Hole1]-Hole1,NewOpenList-NewHole),
			read_letters(NewOpenList-NewHole,Ans,Type,LastCh)
		);
		(Ans-[]=OpenList-Hole,LastCh=Ch)
	).

test_read_letters(File,Word,Type,LastCh) :-
	%close(File),
	see(File),
	read_letters(Hole-Hole,Letters,Type,LastCh),
	atom_codes(Word, Letters),
	close(File).

test_read_words(File,List) :-
	%close(File),
	see(File),
	read_words(List),
	close(File).

add_back(Ch,OpenList-Hole,Ans) :- diff_append(OpenList-Hole,[Ch|X]-X,Ans-[]).
diff_append(OpenList1-Hole1,Hole1-Hole2,OpenList1-Hole2).

test(File,WordsList) :-
	see(File),
	read_tokens(WordsList),
	close(File).

read_tokens(WordsList) :-
	read_letters(Letters,Eof),
	length(Letters,N),
	write(Letters),
	Eof=\=true -> (
		atom_codes(Word, Letters),
		WordsList=[Word|List0],
		read_tokens(List0)
		);
	WordsList=[].

%space, enter, :, ;

